﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Entity;
using PM.Services;
using System.Drawing;

namespace PM.Site.Admin
{
    public partial class ChildrenForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null && int.Parse(Session["UserTypeId"].ToString()) ==1 &&
                Session["ClientId"] != null && Session["ClientId"].ToString() != "")
            {
                this.Loading();

               /* if (Session["ClientId"] != null)
                {
                    this.Loading(Session["ClientId"].ToString());
                }*/


            }
            else
            {
                Response.Redirect("Default.aspx");
            }

        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {

                ddlTitleList.DataSource = PersonUtil.GetTitleCollection();
                ddlTitleList.DataBind();
                ddlGender.DataSource = PersonUtil.GetGenderCollection();
                ddlGender.DataBind();
                ddlMaritalStatus.DataSource = PersonUtil.GetMartialStatusCollection();
                ddlMaritalStatus.DataBind();
                if (Session["ClientId"] != null && Session["ClientId"].ToString() != "")
                {
                    grvChildren.DataSource = ChildServices.GetChildrenByClientId(Session["ClientId"].ToString());
                    grvChildren.DataBind();
                    txbClientId.Text = Session["ClientId"].ToString();                    
                }
            }
        }

        private void Loading(string client_id)
        {
            if (!Page.IsPostBack)
            {
                /* Child child = ChildServices.GetChildByKeys(client_id,null);
                 txbClientId.Text = child.ClientId;
                 txbClientId.ReadOnly = true;
                 txbChildId.Text = child.ChildId;
                 txbChildId.ReadOnly = true;
                 ddlTitleList.SelectedValue = child.Title;
                 txbFullName.Text = child.Other_Names;
                 txbDob.Text = child.DoB.ToShortDateString();
                 ddlGender.SelectedValue = child.Gender;
                 txbMartialStatus.Text = child.MaritalStatus;
                 txbAddRes.Text = child.AddressRes;
                 txbAddOffice.Text = child.AddressOff;
                 txbTelMobile.Text = child.TeleMob;
                 txbTelRes.Text = child.TeleRes;
                 txbTelOffice.Text = child.TeleOff;
                 txbFax.Text = child.Fax;
                 txbEmail.Text = child.Email;
                 txbOccupation.Text = child.Occupation;
                 txbRemarks.Text = child.Remarks;
                 * */
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Child child = new Child();
                child.ClientId = txbClientId.Text;
                child.ChildId = txbChildId.Text;
                child.Title = ddlTitleList.SelectedValue;
                child.Other_Names = txbFullName.Text;
                DateTime dob = DateTime.MinValue;
                if (DateTime.TryParse(txbDob.Text, out dob))
                {
                    child.DoB = dob;
                }
                child.Gender = ddlGender.SelectedValue;
                child.MaritalStatus = ddlMaritalStatus.SelectedValue;
                child.AddressRes = txbAddRes.Text;
                child.AddressOff = txbAddOffice.Text;
                child.TeleMob = txbTelMobile.Text;
                child.TeleRes = txbTelRes.Text;
                child.TeleOff = txbTelOffice.Text;
                child.Fax = txbFax.Text;
                child.Email = txbEmail.Text;
                child.Occupation = txbOccupation.Text;
                child.Remarks = txbRemarks.Text;
                ChildServices.Add(child);
                
                lblMsg.Text = "Successfully saved.";
                lblMsg.ForeColor = Color.Green;
            }
            catch (Exception except)
            {
                lblMsg.ForeColor = Color.Red;
                lblMsg.Text = "Details were not saved properly. Please try again later.";
            }
        }
    }
}
